﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Black_Jack
{
    public partial class Form1 : Form
    {   //Card aaa = new Card();
        //analyz bbb = new analyz();
        private IPEndPoint ipEndPt1;
        private Socket NewClient;
        private Socket clientSok;
        string otvet;
        string mes;
        int Stek = 100, Stavka=1;
        string stProtivnik;
        private int pt1 = 12345, s1=0;
        analyz aaa = new analyz();
        Socket sListen;
        private Thread ThServ;
        int asd, ot=0;
        public int flag = 0, us=0, fl=0;
        public Form1()
        {
            
            InitializeComponent();
        }

        private void Listen()
        {
            sListen.Listen(10);
            clientSok = sListen.Accept();
            Thread t = new Thread(new ThreadStart(listserv));
            t.Start();
        }


        void listcl()
        {
            int[] play = new int[9];
            int i, s2=0, d=0;
            while (true)
            {
                mes = NewClient.Receive();
                NewClient.Send(textBox1.Text);
                stProtivnik = NewClient.Receive();
                NewClient.Send(textBox3.Text);
                i = 0;
                while (i < 9)
                {
                    asd = Convert.ToInt32(NewClient.Receive());
                    NewClient.Send(asd.ToString());
                    play[i] = asd;
                    i++;
                }
                //pictureBox3.Image = imageList1.Images[play[2]];
                //pictureBox4.Image = imageList1.Images[play[3]];
                //pictureBox5.Image = imageList1.Images[play[4]];
                //pictureBox6.Image = imageList1.Images[play[5]];
                //pictureBox7.Image = imageList1.Images[play[6]];
                pictureBox8.Image = imageList1.Images[play[7]];
                pictureBox9.Image = imageList1.Images[play[8]];
                //request.Y = Convert.ToInt32(NewClient.Receive());
                //NewClient.Send(Convert.ToString(pictureBox3.Top));
                if (fl == 0)
                {
                    int a, b;
                    a = Convert.ToInt32(textBox3.Text);
                    b = Convert.ToInt32(stProtivnik);
                    if ((a == b) && (a != 0) && (s2 == 0) && (flag == 1) && (b != 1000))
                    {
                        pictureBox3.Visible = true;
                        pictureBox4.Visible = true;
                        pictureBox5.Visible = true;
                        pictureBox3.Image = imageList1.Images[play[2]];
                        pictureBox4.Image = imageList1.Images[play[3]];
                        pictureBox5.Image = imageList1.Images[play[4]];
                        textBox6.Text = Convert.ToString(a + b);
                        textBox1.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) - a);
                        flag = 0;
                        s2 = 1;
                    }
                    a = Convert.ToInt32(textBox3.Text);
                    b = Convert.ToInt32(textBox4.Text);
                    if ((a == b) && (a != 0) && (s2 == 1) && (flag == 1) && (b != 1000))
                    {
                        pictureBox6.Visible = true;
                        pictureBox6.Image = imageList1.Images[play[5]];
                        textBox6.Text = Convert.ToString(a + b + Convert.ToUInt32(textBox6.Text));
                        textBox1.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) - a);
                        flag = 0;
                        s2 = 2;
                    }
                    a = Convert.ToInt32(textBox3.Text);
                    b = Convert.ToInt32(textBox4.Text);
                    if ((a == b) && (a != 0) && (s2 == 2) && (flag == 1) && (b != 1000))
                    {
                        pictureBox7.Visible = true;
                        pictureBox7.Image = imageList1.Images[play[6]];
                        textBox6.Text = Convert.ToString(a + b + Convert.ToUInt32(textBox6.Text));
                        textBox1.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) - a);
                        flag = 0;
                        s2 = 3;
                    }
                    a = Convert.ToInt32(textBox3.Text);
                    b = Convert.ToInt32(textBox4.Text);
                    if ((a == b) && (a != 0) && (s2 == 3) && (flag == 1) && (b != 1000))
                    {
                        pictureBox1.Visible = true;
                        pictureBox2.Visible = true;
                        pictureBox1.Image = imageList1.Images[play[0]];
                        pictureBox2.Image = imageList1.Images[play[1]];
                        textBox6.Text = Convert.ToString(a + b + Convert.ToUInt32(textBox6.Text));
                        textBox1.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) - a);
                        s2 = 4;
                    }
                    if (((s2 == 4) && (flag == 1)) || (b == 1000))
                    {
                        flag = 0;
                        d = Convert.ToInt32(NewClient.Receive());
                        if ((d == 2) || (b == 1000))
                        {
                            textBox1.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox6.Text));
                        }
                        if (d == 0)
                        {
                            textBox1.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) + (Convert.ToInt32(textBox6.Text) / 2));
                        }
                        //timer3.Start();

                    }
                }
            }
        }

        void listserv()
        {
            int i;
            
            while (true)
            {
                clientSok.Send(textBox1.Text);
                otvet = clientSok.Receive();
                clientSok.Send(textBox3.Text);
                stProtivnik = clientSok.Receive();
                i = 0;
                while (i<9)
                {
                    clientSok.Send(Convert.ToString(aaa.razd[i]));
                    clientSok.Receive();
                    i++;
                }
                //clientSok.Send();
                //request1.Y = Convert.ToInt32(clientSok.Receive());
              

            }

            
        }

        private void Form1_Load(object sender, EventArgs e)
        {int k;
        //bbb.karta();
        
        textBox1.Text = Convert.ToString(Stek);
        textBox3.Text = "0";
        textBox4.Text = "0";
        textBox5.Text = "1";
        textBox6.Text = "0";
        ipEndPt1 = new IPEndPoint(IPAddress.Loopback, pt1);
        //    aaa.karta();
        //aaa.kombo1();
        //   pictureBox1.Image = imageList1.Images[aaa.razd[0]];
        //   pictureBox2.Image = imageList1.Images[aaa.razd[1]];
        //   pictureBox3.Image = imageList1.Images[aaa.razd[2]];
        //   pictureBox4.Image = imageList1.Images[aaa.razd[3]];
        //   pictureBox5.Image = imageList1.Images[aaa.razd[4]];
        //   pictureBox6.Image = imageList1.Images[aaa.razd[5]];
        //   pictureBox7.Image = imageList1.Images[aaa.razd[6]];
          

          // pictureBox8.Image = imageList1.Images[11];
          // textBox1.Text=Convert.ToString(aaa.karta());
          // textBox1.Text = Convert.ToString(aaa.razd[0]);
        }

        public void button4_Click(object sender, EventArgs e)
        {
            fl = 0;
            if (us == 0)
            {
                sListen = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);

                sListen.Bind(ipEndPt1);
                ThServ = new Thread(new ThreadStart(Listen));
                ThServ.Start();
            }
            us = 1;
            timer2.Start();
            aaa.karta();
            aaa.kombo1();
            aaa.kombo2();
            ot = aaa.rez();
            pictureBox1.Image = imageList1.Images[aaa.razd[0]];
            pictureBox2.Image = imageList1.Images[aaa.razd[1]];
            
         
            
           // pictureBox6.Image = imageList1.Images[aaa.razd[5]];
            //pictureBox7.Image = imageList1.Images[aaa.razd[6]];

        }

        public void button5_Click(object sender, EventArgs e)
        {
            fl = 0;

            if (us == 0)
            {
                try
                {

                    NewClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                }
                catch
                {
                    MessageBox.Show("ppc");
                }
                NewClient.Connect(ipEndPt1);
                Thread t = new Thread(new ThreadStart(listcl));
                t.Start();
            }
            us = 2;
            timer1.Start();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            textBox2.Text = mes;
            if ((stProtivnik == "1000")&&(fl ==0))
            {
                fl = 1;
                textBox1.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox6.Text));
            }
            else
                if (fl == 0) 
                {
                    if (stProtivnik == null)
                    {
                        textBox4.Text = "0";
                    }
                    else
                    {
                        textBox4.Text = stProtivnik;
                    }
                }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            textBox2.Text = otvet;
            if ((stProtivnik == "1000")&&(fl ==0))
            {
                fl = 1;
                textBox1.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox6.Text));
            }
            else
                if (fl == 0)
            {
                if (stProtivnik == null)
                {
                    textBox4.Text = "0";
                }
                else
                {
                    textBox4.Text = stProtivnik;
                }
                int a, b, c, d = 0;
                a = Convert.ToInt32(textBox3.Text);
                b = Convert.ToInt32(textBox4.Text);
                if ((a == b) && (b != 0) && (s1 == 0) && (flag == 1) && (b != 1000))
                {
                    pictureBox3.Visible = true;
                    pictureBox4.Visible = true;
                    pictureBox5.Visible = true;
                    pictureBox3.Image = imageList1.Images[aaa.razd[2]];
                    pictureBox4.Image = imageList1.Images[aaa.razd[3]];
                    pictureBox5.Image = imageList1.Images[aaa.razd[4]];
                    c = Convert.ToInt32(textBox1.Text);
                    textBox6.Text = Convert.ToString(a + b);
                    textBox1.Text = Convert.ToString(c - b);
                    flag = 0;
                    s1 = 1;
                }
                a = Convert.ToInt32(textBox3.Text);
                b = Convert.ToInt32(textBox4.Text);
                if ((a == b) && (b != 0) && (s1 == 1) && (flag == 1) && (b != 1000))
                {
                    pictureBox6.Visible = true;
                    pictureBox6.Image = imageList1.Images[aaa.razd[5]];
                    textBox6.Text = Convert.ToString(a + b + Convert.ToUInt32(textBox6.Text));
                    textBox1.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) - a);
                    flag = 0;
                    s1 = 2;
                }
                a = Convert.ToInt32(textBox3.Text);
                b = Convert.ToInt32(textBox4.Text);
                if ((a == b) && (b != 0) && (s1 == 2) && (flag == 1) && (b != 1000))
                {
                    pictureBox7.Visible = true;
                    pictureBox7.Image = imageList1.Images[aaa.razd[6]];
                    textBox6.Text = Convert.ToString(a + b + Convert.ToUInt32(textBox6.Text));
                    textBox1.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) - a);
                    flag = 0;
                    s1 = 3;
                }
                a = Convert.ToInt32(textBox3.Text);
                b = Convert.ToInt32(textBox4.Text);
                if ((a == b) && (b != 0) && (s1 == 3) && (flag == 1) && (b != 1000))
                {
                    pictureBox8.Visible = true;
                    pictureBox9.Visible = true;
                    pictureBox8.Image = imageList1.Images[aaa.razd[7]];
                    pictureBox9.Image = imageList1.Images[aaa.razd[8]];
                    textBox6.Text = Convert.ToString(a + b + Convert.ToUInt32(textBox6.Text));
                    textBox1.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) - a);
                    s1 = 4;
                }
                if ((s1 == 4) && (flag == 1))
                {
                    flag = 0;
                    clientSok.Send(Convert.ToString(aaa.rez()));
                    d = aaa.rez();
                    if (d == 1)
                    {
                        textBox1.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox6.Text));
                    }
                    if (d == 0)
                    {
                        textBox1.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) + (Convert.ToInt32(textBox6.Text) / 2));
                    }
                }
            }
            

        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            textBox5.Text = trackBar1.Value.ToString();
            trackBar1.Maximum=Convert.ToInt32(textBox1.Text);
               
        }

        private void button1_Click(object sender, EventArgs e)
        {
           

        }

        private void timer3_Tick(object sender, EventArgs e)
        {
                  }

        private void label5_Click(object sender, EventArgs e)
        {
                    }

        private void button9_Click(object sender, EventArgs e)
        {
            flag = 1;
            textBox3.Text = textBox5.Text;
        }

        private void button6_Click(object sender, EventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (us == 1)
            {
                button4_Click(sender, e);
      
                pictureBox3.Visible = false;
                pictureBox4.Visible = false;
                pictureBox5.Visible = false;
                pictureBox6.Visible = false;
                pictureBox7.Visible = false;
                pictureBox8.Visible = false;
                pictureBox9.Visible = false;
            }
            if (us == 2)
            {
                button5_Click(sender, e);            
                pictureBox3.Visible = false;
                pictureBox4.Visible = false;
                pictureBox5.Visible = false;
                pictureBox6.Visible = false;
                pictureBox7.Visible = false;
                pictureBox1.Visible = false;
                pictureBox2.Visible = false;
            }
            textBox3.Text = "0";
            textBox4.Text = "0";
            textBox5.Text = "1";
            textBox6.Text = "0";
            
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox3.Text = "1000";
        }



    }

    public static class SocketExtensions
    {
        public static string Receive(this Socket socket)
        {
            var buffer = new byte[1024];
            var count = socket.Receive(buffer);
            return Encoding.Default.GetString(buffer, 0, count);
        }

        public static void Send(this Socket socket, string message)
        {
            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentNullException();
            }
            socket.Send(Encoding.Default.GetBytes(message));
        }
    }
}